
define(["lazyLoad", "page", "nav"], function (lazyLoad, renderPagination, nav) {
    'use strict'
    let result = "";
    let find = "";
    let subject_id = "";
    let catId = '';
    var showData = 20;
    function getProductListData(subject_id) {
        catId = getCatId(subject_id);
        // let url = "pxx/";
        // let data = {
        //     subject_id: subject_id,
        //     page: 1,
        //     size: 40,
        //     // province_id: 25,
        //     // platform: 1,
        //     // pdduid: 0
        // }

        let url = "http://zhaoyx0907.com/pxx";
        $.ajax(url,
            {
                // data: data,
                dataType: "json"
            }
        ).then(upDataHtml, function (error) {
            result = JSON.parse(localStorage.getItem("list"));
            find = result.filter((item) => {
                return item.cat_id1 == catId;
            })
            render();
        })
    }
    function upDataHtml(res){
        result = res.goods_list;
        localStorage.setItem("list", JSON.stringify(result))
        if (catId == 9999) {
            find = result;
        } else {
            find = result.filter((item) => {
                return item.cat_id1 == catId;
            })
        }
        render();
    }
    function renderHtml(current = 1) {
        var container = $(".productList");
        var reduce = 3;
        var html = "";
        // for (var i = 0; i < res.length; i++) {

        for (var i = (current - 1) * showData; i < current * showData; i++) {
            var last = i % 5 === 0 ? "last" : "";
            var firstLine = i < 5 ? "firstLine" : "";
            if (!find[i])
                break;
            html = html +
                `<div class=" ${last} ${firstLine} product" data-id="${find[i].goods_id}">
                    <div class="productimg">
                        <a href="./detail.html?${find[i].goods_id}"><img
                                    src="./images/juanpi.jpg"
                                    alt="" data-src="${find[i].hd_thumb_url}"></a>
                        <span class="supportContainer" title="加入收藏"><i
                                    class="iconfont icon_spport">&#xe611;</i></span>
                    </div>
                    <div class="productPrice">
                        <span class="productPrice_now">￥${find[i].normal_price / 100}</span>
                        <strong class="productPrice_old">￥${(find[i].market_price / 100)}</strong>
                    </div>
                    <div class="productTitle">
                        <span class="title">${find[i].short_name}</span>
                        <strong class="status">剩${reduce}天</strong>
                    </div>
                </div>`
        }

        container.html(html);
    }
    function render() {
        $(".loading").css({
            display:"none"
        })
        renderProductList(1);
        renderPagination(renderProductList, find.length - 1, showData);
    }
    function renderProductList(current) {
        renderHtml(current);
        lazyLoad(".productimg img");

    }
    function product(s_id) {
        //省略了后台请求数据总数的过程,直接默认100条
        subject_id = s_id;
        getProductListData(subject_id);
    }
    function changeProductUrl(elem) {
        var subject_id = $(elem).attr("data-id");
        var cat_id = getCatId(subject_id);
        find = result.filter((item) => {
            return item.cat_id1 == cat_id;
        });
        render();
        $(elem).find("a").css({
            color: "red",
            "text-decoration": "none"
        })
    }
    (function () {
        var location = window.location.href;
        var _id = location.split("?")[1];
        var subject_id = _id ? _id.split("#")[0] : _id;
        product(subject_id);
        nav(".home_head", "li", changeProductUrl);
    })()
    // return product;

    function getCatId(subject_id) {
        var cat_id = "";
        switch (subject_id + '') {
            case ('5575'):
                cat_id = 17285;//"百货";
                break;
            case ("5577"):
                cat_id = 5834;//"电器";
                break;
            case ("5579"):
                cat_id = 9319;//"家纺";
                break;
            case ("5573"):
                cat_id = 6785;//"美食";
                break;
            case ("5576"):
                cat_id = 18637;//"美妆";
                break;
            case ("5578"):
                cat_id = 14933;//"母婴";
                break;
            case ("5572"):
                cat_id = 239;//"男装";
                break;
            case ("5571"):
                cat_id = 8439;//"女装";
                break;
            case ("5574"):
                cat_id = 8509;//"鞋包";
                break;
            default:
                cat_id = 9999;
                break;
        }
        return cat_id;

    }
})

    // function showProductDetail(elem) {
    //     var goods_id = $(elem).attr("data-id");

    //     // window.location.href="./detail.html";
    //     // var w=window.open("./detail.html");
    //     // w.obj=goods_id;

    // }